@namespace Oqtane.Modules.Admin.Login
@inherits ModuleBase
@inject NavigationManager NavigationManager
@inject IUserService UserService
@inject IServiceProvider ServiceProvider

@if (_message != string.Empty)
{
    <ModuleMessage Message="@_message" Type="@_type" />
}
<AuthorizeView>
    <Authorizing>
        <text>...</text>
    </Authorizing>
    <Authorized>
        <ModuleMessage Message="You Are Already Logged In" Type="MessageType.Info" />
    </Authorized>
    <NotAuthorized>
        <div class="container Oqtane-Modules-Admin-Login">
            <div class="form-group">
                <label for="Username" class="control-label">Username: </label>
                <input type="text" name="Username" class="form-control username" placeholder="Username" @bind="@_username" id="Username" />
            </div>
            <div class="form-group">
                <label for="Password" class="control-label">Password: </label>
                <input type="password" name="Password" class="form-control password" placeholder="Password" @bind="@_password" id="Password" />
            </div>
            <div class="form-group">
                <div class="form-check form-check-inline">
                    <label class="form-check-label" for="Remember">Remember Me?</label>&nbsp;
                    <input type="checkbox" class="form-check-input" name="Remember" @bind="@_remember" id="Remember" />
                </div>
            </div>
            <button type="button" class="btn btn-primary" @onclick="Login">Login</button>
            <button type="button" class="btn btn-secondary" @onclick="Cancel">Cancel</button>
            <br /><br />
            <button type="button" class="btn btn-secondary" @onclick="Forgot">Forgot Password</button>
        </div>
    </NotAuthorized>
</AuthorizeView>

@code {
    private string _returnUrl = string.Empty;
    private string _message = string.Empty;
    private MessageType _type = MessageType.Info;
    private string _username = string.Empty;
    private string _password = string.Empty;
    private bool _remember = false;

    public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Anonymous;

    public override List<Resource> Resources => new List<Resource>()
    {
        new Resource { ResourceType = ResourceType.Stylesheet, Url = ModulePath() + "Module.css" }
    };

    protected override async Task OnInitializedAsync()
    {
        if (PageState.QueryString.ContainsKey("returnurl"))
        {
            _returnUrl = PageState.QueryString["returnurl"];
        }

        if (PageState.QueryString.ContainsKey("name"))
        {
            _username = PageState.QueryString["name"];
        }

        if (PageState.QueryString.ContainsKey("token"))
        {
            var user = new User();
            user.SiteId = PageState.Site.SiteId;
            user.Username = _username;
            user = await UserService.VerifyEmailAsync(user, PageState.QueryString["token"]);

            if (user != null)
            {
                _message = "User Account Verified Successfully. You Can Now Login With Your Username And Password Below.";
            }
            else
            {
                _message = "User Account Could Not Be Verified. Please Contact Your Administrator For Further Instructions.";
                _type = MessageType.Warning;
            }
        }
    }

    private async Task Login()
    {
        if (PageState.Runtime == Runtime.Server)
        {
            // server-side Blazor
            var user = new User();
            user.SiteId = PageState.Site.SiteId;
            user.Username = _username;
            user.Password = _password;
            user = await UserService.LoginUserAsync(user, false, false);

            if (user.IsAuthenticated)
            {
                await logger.LogInformation("Login Successful For Username {Username}", _username);
                // complete the login on the server so that the cookies are set correctly on SignalR
                var interop = new Interop(JSRuntime);
                string antiforgerytoken = await interop.GetElementByName("__RequestVerificationToken");
                var fields = new { __RequestVerificationToken = antiforgerytoken, username = _username, password = _password, remember = _remember, returnurl = _returnUrl };
                await interop.SubmitForm($"/{PageState.Alias.AliasId}/pages/login/", fields);
            }
            else
            {
                await logger.LogInformation("Login Failed For Username {Username}", _username);
                AddModuleMessage("Login Failed. Please Remember That Passwords Are Case Sensitive And User Accounts Require Email Verification When They Initially Created.", MessageType.Error);
            }
        }
        else
        {
            // client-side Blazor
            var user = new User();
            user.SiteId = PageState.Site.SiteId;
            user.Username = _username;
            user.Password = _password;
            user = await UserService.LoginUserAsync(user, true, _remember);
            if (user.IsAuthenticated)
            {
                await logger.LogInformation("Login Successful For Username {Username}", _username);
                var authstateprovider = (IdentityAuthenticationStateProvider)ServiceProvider.GetService(typeof(IdentityAuthenticationStateProvider));
                authstateprovider.NotifyAuthenticationChanged();
                NavigationManager.NavigateTo(NavigateUrl(_returnUrl, "reload"));
            }
            else
            {
                await logger.LogInformation("Login Failed For Username {Username}", _username);
                AddModuleMessage("Login Failed. Please Remember That Passwords Are Case Sensitive And User Accounts Require Verification When They Are Initially Created So You May Wish To Check Your Email.", MessageType.Error);
            }
        }
    }

    private void Cancel()
    {
        NavigationManager.NavigateTo(_returnUrl);
    }

    private async Task Forgot()
    {
        if (_username != string.Empty)
        {
            var user = await UserService.GetUserAsync(_username, PageState.Site.SiteId);
            if (user != null)
            {
                await UserService.ForgotPasswordAsync(user);
                _message = "Please Check The Email Address Associated To Your User Account For A Password Reset Notification";
            }
            else
            {
                _message = "User Does Not Exist";
                _type = MessageType.Warning;
            }
        }
        else
        {
            _message = "Please Enter The Username Related To Your Account And Then Select The Forgot Password Option Again";
        }

        StateHasChanged();
    }
}
